package com.mall.web.control.app.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.mall.biz.item.MallItemBizImpl;
import com.mall.biz.mall.ZhiHuiMallBiz;
import com.mall.client.item.ItemCategoryService;
import com.mall.client.item.TableNumService;
import com.mall.client.mall.MallService;
import com.mall.pojo.ItemCategory;
import com.mall.pojo.Mall;
import com.mall.pojo.MallItem;
import com.mall.pojo.MallItemOrder;
import com.mall.pojo.TableNum;
import com.mall.pojo.User;
import com.mall.vo.item.MallItemAndCategoryVO;
import com.mall.vo.item.MallItemVO;
import com.mall.web.annotation.UserLoginMessage;
import com.mall.web.util.APPCookieUtil;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;

@Controller
@RequestMapping("/zhihui/")
public class ZhihuiMallController {
	
	private static final Logger log = LoggerFactory.getLogger(ZhihuiMallController.class);
	
	@Autowired
	private MallService mallService;
	@Autowired
	private TableNumService tableNumService;
	@Autowired
	private ItemCategoryService itemCategoryService;
	@Autowired
	private ZhiHuiMallBiz zhiHuiMallBiz;
	
	private MallItemBizImpl mallItemBiz;
	
	@RequestMapping("zhihuiMallDetail")
	@UserLoginMessage
	public String zhihuiMallDetail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		User user = WebUtil.getCurrentUser(request);
		String tableNumStr = HttpUtil.getStringValueFromRequest(request, "tableNum");
		Long mallId = HttpUtil.getLongValueFromRequest(request, "mallId");
		Mall mall = this.mallService.getMallById(mallId);
		if (mall.getSsig() != 1) {
			log.error("该店不是智慧餐厅");
			return "redirect:/mall/list/mallDetail.htm?mallId=" + mall.getId();
		}
		TableNum tableNum = null;
		if (StringUtils.isNotBlank(tableNumStr)) {
			tableNum = this.tableNumService.getTableNumByMallIdAndTable(mallId, tableNumStr,onlyKey);
		}
		int app = APPCookieUtil.urlFromApp(request);
		modelMap.put("app", app);
		modelMap.put("tableNum", tableNum);
		modelMap.put("mallId", mallId);
		modelMap.put("mall", mall);
		modelMap.put("user", user);

		// 判断是否有订单存在 start
		if (user != null) {
			MallItemOrder mallItemOrder = null;
			List<MallItemVO> userZhiHuiTradeDetailVOList = null;
			mallItemOrder = zhiHuiMallBiz.getTodayMallFireOrderByUserAndTime(mallId, user.getId());
			if (mallItemOrder != null) {
				String mallItemListJson = mallItemOrder.getBuyItemsJson();
				if (mallItemListJson != null) {
					userZhiHuiTradeDetailVOList = JSONArray.parseArray(mallItemListJson, MallItemVO.class);
				}
				modelMap.put("userZhiHuiTradeDetailVOList", userZhiHuiTradeDetailVOList);
				modelMap.put("mallItemOrder", mallItemOrder);
			}

		}
		// end

		List<ItemCategory> itemCategoryList = this.itemCategoryService.getItemCategoryListByMallId(mallId,onlyKey);

		if (itemCategoryList == null || itemCategoryList.size() <= 0) {//
			return "mall/zhihui/zhihuiMallDetail";
		}

		// 获取所有商品数据
		List<MallItem> allMallItemList = this.mallItemBiz.getMallItemListByMallIdAndOnlykey(mallId, onlyKey);
		// 获取所有类目数据
		List<MallItemAndCategoryVO> mallItemAndCategoryVOList = new ArrayList<>();
		MallItemAndCategoryVO mallItemAndCategoryVO = null;
		Map<Long, MallItemAndCategoryVO> mallItemAndCategoryVOMap = new HashMap<>();

		for (ItemCategory itemCategory : itemCategoryList) {// init map
			mallItemAndCategoryVO = new MallItemAndCategoryVO();
			mallItemAndCategoryVO.setItemCategory(itemCategory);
			mallItemAndCategoryVO.setMallItemList(null);
			mallItemAndCategoryVOMap.put(itemCategory.getId(), mallItemAndCategoryVO);
		}

		if (allMallItemList != null && allMallItemList.size() > 0) {
			for (MallItem mallItem : allMallItemList) {
				Long category = mallItem.getItemCategoryId();
				mallItemAndCategoryVO = mallItemAndCategoryVOMap.get(category);
				if (mallItemAndCategoryVO == null) {
					continue;
				}
				List<MallItem> mapMallItemList = mallItemAndCategoryVO.getMallItemList();
				if (mapMallItemList == null || mapMallItemList.size() <= 0) {
					mapMallItemList = new ArrayList<>();
				}
				mapMallItemList.add(mallItem);
				mallItemAndCategoryVO.setMallItemList(mapMallItemList);
			}
		}

		for (Map.Entry<Long, MallItemAndCategoryVO> entry : mallItemAndCategoryVOMap.entrySet()) {
			mallItemAndCategoryVOList.add(entry.getValue());
		}

		modelMap.put("mallItemAndCategoryVOList", mallItemAndCategoryVOList);
		modelMap.put("itemCategoryList", itemCategoryList);
		String referer = request.getHeader("Referer");
		modelMap.put("referer", referer);
		return "mall/zhihui/zhihuiMallDetail";
		
	}

}
